package primes

import (
	"fmt"
	"strconv"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/otiai10/primes"
)

func UI(a fyne.App) {
	w := a.NewWindow("Find primes, factorize numbers and reduce fractions by Golang.")
	w.Resize(fyne.NewSize(600, 600))
	input := widget.NewEntry()
	input.SetText("20")
	output := widget.NewEntry()
	c1 := container.NewVBox(
		input,
		container.NewHBox(
			widget.NewButton("求质数", func() {
				inputNum, err := strconv.Atoi(input.Text)
				if err != nil {
					output.SetText("please input an integer")
					return
				}
				output.SetText(fmt.Sprint(primes.Until(int64(inputNum)).List()))
			}),
			widget.NewButton("因式分解", func() {
				inputNum, err := strconv.Atoi(input.Text)
				if err != nil {
					output.SetText("please input an integer")
					return
				}
				output.SetText(fmt.Sprint(primes.Factorize(int64(inputNum)).All()))
			}),
			widget.NewButton("因式分解2", func() {
				inputNum, err := strconv.Atoi(input.Text)
				if err != nil {
					output.SetText("please input an integer")
					return
				}
				output.SetText(fmt.Sprint(primes.Factorize(int64(inputNum)).Powers()))
			}),
			widget.NewButton("约分", func() {
				ss := strings.Split(input.Text, ",")
				if len(ss) != 2 {
					output.SetText("please input two nums joined by ,")
					return
				}
				a, _ := strconv.Atoi(ss[0])
				b, _ := strconv.Atoi(ss[1])
				output.SetText(fmt.Sprint(primes.Fractionize(int64(a), int64(b)).Reduce(20)))
			}),
			widget.NewButton("分数", func() {
				ss := strings.Split(input.Text, ",")
				if len(ss) != 2 {
					output.SetText("please input two nums joined by ,")
					return
				}
				a, _ := strconv.Atoi(ss[0])
				b, _ := strconv.Atoi(ss[1])
				output.SetText(primes.Fractionize(int64(a), int64(b)).String())
			}),
		),
		output,
	)
	w.SetContent(c1)
	w.CenterOnScreen()
	w.Show()
}

/*
func main() {
	fmt.Println(
		primes.Until(20).List(),
		primes.Factorize(144).All(),
		primes.Factorize(144).Powers(),
	)
// [2 3 5 7 11 13 17 19]
// [2 2 2 2 3 3]
// map[2:4 3:2]
}
*/
